using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UI;

public class Job1ShopView : MonoBehaviour
{
    public Transform ToggleGroup, ShopItemGroup, MaxTipePanle;
    public Toggle toggle;
    public Job1ShopItem shopItem, TepmItem;
    public TMP_Text MinLogeText, Timetext, DesText, NumText, BuyNumText;
    public Image MinIcon, MaxBgIcon, Icon, ToggleImage;
    List<ShopData> shopDatas;
    List<Job1ShopItem> shopItems = new List<Job1ShopItem>();
    int num=1;
    int toggle_Index;
    // Start is called before the first frame update
    void Start()
    {
        shopDatas = JsonConvert.DeserializeObject<List<ShopData>>(Resources.Load<TextAsset>("ShopData").text);

        shopItem.gameObject.SetActive(false);

        for (int i = 0; i < 6; i++)
        {
            toggle.transform.GetChild(0).GetComponent<Image>().sprite = Resources.Load<Sprite>("资源切图/tab页icon/" + (++toggle_Index).ToString());

            toggle.transform.GetChild(0).transform.GetChild(0).GetComponent<Image>().sprite = Resources.Load<Sprite>("资源切图/tab页icon/" + (++toggle_Index).ToString());

            int index = i;

            var tog = Instantiate(toggle, ToggleGroup);

            tog.onValueChanged.AddListener((b) =>
            {
                if (b)
                {
                    ChangeData(index);
                }
            });
            if (i != 5)
            {
                Instantiate(ToggleImage, ToggleGroup);
            }
        }
        toggle.gameObject.SetActive(false);
        ToggleImage.gameObject.SetActive(false);

        ChangeData(0);
         BuyNumText.text =  num.ToString("#,###.##");
    }

    private void ChangeList(List<ShopData> shopDatas)
    {
        for (int i = 0; i < shopDatas.Count; i++)
        {
            GetItem(shopDatas[i], i);
        }
    }

    private void ChangeData(int index)
    {
        HideAllList();

        List<ShopData> datas = new List<ShopData>();

        foreach (var item in shopDatas)
        {
            if (item.type == (GoodsType)index)
            {
                datas.Add(item);
            }
        }
        ChangeList(datas);
    }

    private void HideAllList()
    {
       for(int i = 0;i < shopItems.Count; i++)
        {
            shopItems[i].gameObject.SetActive(false);
        }
    }

    private void GetItem(ShopData shopData, int i)
    {
        if (shopItems.Count <= i)
        {
            shopItems.Add(Instantiate(shopItem, ShopItemGroup));
        }
        shopItems[i].gameObject.SetActive(true);

        shopItems[i].init(shopData, this);
    }
    public void SetItem(Job1ShopItem job1Shop)
    {
        if (TepmItem != null)
        {
            TepmItem.SetBg(false);
        }
        TepmItem = job1Shop;
        TepmItem.SetBg(true);
        MaxTipePanle.gameObject.SetActive(true);
        MinLogeText.text = TepmItem.My_shopData.name;
        MinIcon.sprite = Resources.Load<Sprite>(GetLogoMinIcon(TepmItem.My_shopData.Level));
        MaxBgIcon.sprite = Resources.Load<Sprite>(GetMaxBgIcon(TepmItem.My_shopData.Level));
        Icon.sprite = Resources.Load<Sprite>(TepmItem.My_shopData.icon);
        Timetext.text = "◷18天12小时";
            //"兑换时间: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        DesText.text = TepmItem.My_shopData.description;
        NumText.text = "可兑换:" + TepmItem.My_shopData.quantity + "/" + TepmItem.My_shopData.max_quantity+"\t\t当前持有:0";
        BuyNumText.text = num.ToString("#,###.##");

    }
    public void LeftBut()
    {
        num--;
        if (num < 1)
        {
            num = 1;
        }
        BuyNumText.text =  num.ToString("#,###.##");
    }
    public void RightBut()
    {
        num++;
        if (num >= TepmItem.My_shopData.max_quantity)
        {
            num = TepmItem.My_shopData.max_quantity;
        }
        BuyNumText.text = num.ToString("#,###.##");
    }
    public void MaxBut()
    {
        num = TepmItem.My_shopData.max_quantity;
        BuyNumText.text = num.ToString("#,###.##");
    }
    // Update is called once per frame
    void Update()
    {

    }
    public string GetMaxBgIcon(int leven)
    {
        switch (leven)
        {
            case 1:
                return "资源切图/云_白";
            case 2:
                return "资源切图/云_绿";
            case 3:
                return "资源切图/云_蓝";
            case 4:
                return "资源切图/云_紫";
            case 5:
                return "资源切图/云_金";
            default:
                return "";
        }
    }
    public string GetLogoMinIcon(int leven)
    {
        switch (leven)
        {
            case 1:
                return "资源切图/白";
            case 2:
                return "资源切图/绿";
            case 3:
                return "资源切图/蓝";
            case 4:
                return "资源切图/紫";
            case 5:
                return "资源切图/金";
            default:
                return "";
        }
    }
}
public class ShopData
{
    public int id;
    public string name;
    public string description;
    public int quantity;
    public int max_quantity;
    public int price;
    public int currency_type;
    public GoodsType type;
    public int num;
    public string icon;
    public int Level;
    public int current_holdings;
    public butType BuyState;
    public discount discount;
}
public enum GoodsType
{
    卷轴,
    材料,
    丹药,
    符禄,
    工具,
    宝物,
}
public enum butType
{
    可以购买,
    售空,
    暂未开放,
}
public enum discount
{
    兑换,
    折扣,
}

